#include "tictactoe.h"
#include "lvgl.h"
#include "abprun.h"

static lv_obj_t *btn[3][3];         //按钮数组
static lv_obj_t *btn_user_pvc;      //用户界面用于PVC按钮
static lv_obj_t *btn_user_pvp;      //用户界面用于PVP按钮
static lv_obj_t *btn_sente;     //选择先手按钮
static lv_obj_t *btn_gote;      //选择后手按钮
static bool is_who_turn = true;     //用于标记轮到哪一方
static int board[3][3];            //创建棋盘，0表示未落子，-1表示红色落子，1表示蓝色落子
static lv_obj_t *label_top;     //创建胜利显示标签
int game_over = 0;       //比赛结束标志
static int mf_flag = 0;       //对战模式选择，“1”表示PVP，“2”表示sente，“3”表示gote

//回调函数，处理后手事件
static void btn_gote_event_cb(lv_event_t *e)
{
    int i, j, bestmove;
    lv_obj_del(btn_gote);
    lv_obj_del(btn_sente);

    create_tictactoe_ui();

    // bestmove = findBestMove();
    bestmove = 4;
    i = bestmove / 3;
    j = bestmove % 3;

    lv_obj_t *label = lv_label_create(btn[i][j]);
    lv_obj_set_style_bg_color(btn[i][j], lv_color_hex(0x0000ff), LV_STATE_DEFAULT);        //设置背景颜色为蓝色
    lv_label_set_text(label, "O");  // 设置标签为O
    lv_obj_center(label);  // 将标签居中
    lv_obj_add_state(btn[i][j], LV_STATE_DISABLED);    //按钮失能
    board[i][j] = 1;
    is_who_turn = false;
}

//回调函数，处理先手事件
static void btn_sente_event_cb(lv_event_t *e)
{
    lv_obj_del(btn_gote);
    lv_obj_del(btn_sente);
    is_who_turn = false;

    create_tictactoe_ui();
}

//回调函数，处理PVC对战
static void pvc_game_event_cb(lv_event_t *e)
{
    int i, j, bestmove = 0;
    lv_obj_t *target = lv_event_get_target(e);
    lv_obj_t *label = lv_label_create(target);
    lv_obj_set_style_bg_color(target, lv_color_hex(0xff0000), LV_STATE_DEFAULT);        //设置背景颜色为红色
    lv_label_set_text(label, "X");  // 设置标签为X
    lv_obj_center(label);  // 将标签居中
    lv_obj_add_state(target, LV_STATE_DISABLED);    //按钮失能
    which_btn(target);
    transmit();
    is_who_turn = true;
    who_win();
    if (game_over == 0 && is_who_turn)
    {
        bestmove = findBestMove();
        // bestmove++;
        i = bestmove / 3;
        j = bestmove % 3;

        lv_obj_t *label_ai = lv_label_create(btn[i][j]);
        lv_obj_set_style_bg_color(btn[i][j], lv_color_hex(0x0000ff), LV_STATE_DEFAULT);        //设置背景颜色为蓝色
        lv_label_set_text(label_ai, "O");  // 设置标签为O
        lv_obj_center(label_ai);  // 将标签居中
        lv_obj_add_state(btn[i][j], LV_STATE_DISABLED);    //按钮失能
        board[i][j] = 1;
        is_who_turn = false;
        who_win();
    }
}

//回调函数，处理PVP事件
static void btn_pvp_event_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);      //获取触发事件的部件
    display_drop(target);
    which_btn(target);
    who_win();
}

//显示PVP当前落子方交互信息
void display_drop(lv_obj_t *target)
{
    lv_obj_t *label = lv_label_create(target);  // 在按钮上创建标签
    lv_obj_center(label);  // 将标签居中

    if(is_who_turn)
    {
        lv_obj_set_style_bg_color(target, lv_color_hex(0xff0000), LV_STATE_DEFAULT);        //设置背景颜色为红色
        lv_label_set_text(label, "X");  // 设置标签为X
        is_who_turn = false;
    }
    else
    {
        lv_obj_set_style_bg_color(target, lv_color_hex(0x0000ff), LV_STATE_DEFAULT);        //设置背景颜色为蓝色
        lv_label_set_text(label, "O");  // 设置标签为O
        is_who_turn = true;
    }
    //lv_obj_set_style_text_font(target, LV_FONT_MONTSERRAT_30, LV_STATE_DEFAULT);      //修改字体大小
    lv_obj_add_state(target, LV_STATE_DISABLED);    //按钮失能
}

//传递函数
void transmit(void)
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            ismove(board[i][j], i, j);
        }
    }
}

//判断按钮
void which_btn(lv_obj_t *target)
{
    for(int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            if(target == btn[i][j])
            {
                if (is_who_turn)
                {
                    board[i][j] = 1;
                }
                else
                {
                    board[i][j] = -1;
                }
            }
        }
    }
}

//2，创建PVC回调函数
static void pvc_event_cb(lv_event_t * e)
{
    //摧毁当前界面
    lv_obj_del(btn_user_pvc);
    lv_obj_del(btn_user_pvp);

    //创建两个按钮，用于表示先后手，添加回调函数
    btn_gote = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn_gote, 130, 130);
    lv_obj_set_pos(btn_gote, 55, 20);
    lv_obj_add_event_cb(btn_gote, btn_gote_event_cb, LV_EVENT_CLICKED, NULL);
    btn_sente = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn_sente, 130, 130);
    lv_obj_set_pos(btn_sente, 55, 170);
    lv_obj_add_event_cb(btn_sente, btn_sente_event_cb, LV_EVENT_CLICKED, NULL);

    //创建两个标签用于显示交互信息
    lv_obj_t *label_gote = lv_label_create(btn_gote);  // 在按钮上创建标签
    lv_obj_center(label_gote);  // 将标签居中
    lv_label_set_text(label_gote, "Gote");  // 设置标签为Gote,后手
    lv_obj_t *label_sente = lv_label_create(btn_sente);  // 在按钮上创建标签
    lv_obj_center(label_sente);  // 将标签居中
    lv_label_set_text(label_sente, "Sente");  // 设置标签为Sente，先手

    mf_flag = 2;
}

//创建PVP回调函数
static void pvp_event_cb(lv_event_t * e)
{
    //摧毁当前界面
    lv_obj_del(btn_user_pvc);
    lv_obj_del(btn_user_pvp);
    mf_flag = 1;        //选择PVP模式

    create_tictactoe_ui();
}

/*  #作用：1
    创建用户界面，有两个按钮，用于选择PVP or PVC
    #参数：
    disp
*/
void create_user_ui(void)
{
    //创建两个按钮，添加回调函数
    btn_user_pvc = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn_user_pvc, 130, 130);
    lv_obj_set_pos(btn_user_pvc, 55, 20);
    lv_obj_add_event_cb(btn_user_pvc, pvc_event_cb, LV_EVENT_CLICKED, NULL);
    btn_user_pvp = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn_user_pvp, 130, 130);
    lv_obj_set_pos(btn_user_pvp, 55, 170);
    lv_obj_add_event_cb(btn_user_pvp, pvp_event_cb, LV_EVENT_CLICKED, NULL);

    //创建两个标签用于显示交互信息
    lv_obj_t *label_pvc = lv_label_create(btn_user_pvc);  // 在按钮上创建标签
    lv_obj_center(label_pvc);  // 将标签居中
    lv_label_set_text(label_pvc, "PVC");  // 设置标签为PVC
    lv_obj_t *label_pvp = lv_label_create(btn_user_pvp);  // 在按钮上创建标签
    lv_obj_center(label_pvp);  // 将标签居中
    lv_label_set_text(label_pvp, "PVP");  // 设置标签为PVP
}

/*  #作用：
    创建五子棋棋盘，按钮矩阵
    #参数：
    无
*/
void create_tictactoe_ui(void)
{
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            btn[i][j] = lv_btn_create(lv_scr_act());
            lv_obj_set_size(btn[i][j], 75, 75);     //设置每个按钮的大小
            lv_obj_set_pos(btn[i][j], j * 80, i * 80 + 40);     //设置每个按钮的位置
            switch (mf_flag)
            {
            case 1:
                lv_obj_add_event_cb(btn[i][j], btn_pvp_event_cb, LV_EVENT_CLICKED, NULL);       //为PVP添加事件回调函数
                break;
            case 2:
                lv_obj_add_event_cb(btn[i][j], pvc_game_event_cb, LV_EVENT_CLICKED, NULL);       //为PVC添加事件回调函数
                break;
            default:
                ;
                break;
            }
            lv_obj_set_style_bg_color(btn[i][j], lv_color_hex(0x00ff00), LV_STATE_DEFAULT);         //设置背景颜色为绿色
        }
    }
}

/*  #作用：
    判断胜利一方,创建交互信息，摧毁井字棋ui
    #参数：
    无
*/
void who_win(void)
{
    int win_flag = 0;
    int count;
    for(int i = 0; i < 3; i++)
    {
        int x = board[i][0] != 0 && board[i][0] == board[i][1] && board[i][1] == board[i][2];       //行
        int y = board[0][i] != 0 && board[0][i] == board[1][i] && board[1][i] == board[2][i];       //列
        if(x)
        {
            win_flag = board[i][0] > 0 ? 1 : -1;
        }
        if(y)
        {
            win_flag = board[0][i] > 0 ? 1 : -1;
        }
    }
    if((board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1) || (board[0][2] == 1 && board[1][1] == 1 && board[2][0] == 1))
    {
        win_flag = 1;
    }
    if((board[0][0] == -1 && board[1][1] == -1 && board[2][2] == -1) || (board[0][2] == -1 && board[1][1] == -1 && board[2][0] == -1))
    {
        win_flag =  -1;
    }

    count = 0;
    //平局判断
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            if(board[i][j] == 0)
            {
                count++;        //记录空位个数
            }
        }
    }
    if(count == 0 && win_flag == 0)
    {
        win_flag = -2;
    }

    //创建标签用于显示胜利一方
    label_top = lv_label_create(lv_scr_act());
    lv_label_set_text(label_top, "");       //初始显示为空

    if(win_flag == 1 && game_over == 0)
    {
        lv_label_set_text(label_top, "Blue Win!");
        game_over = 1;
    }
    else if (win_flag == -1 && game_over == 0)
    {
        lv_label_set_text(label_top, "Red Win!");
        game_over = 1;
    }
    else if (win_flag == -2 && game_over == 0)
    {
        lv_label_set_text(label_top, "Dogfall!");
        game_over = 1;
    }
    lv_obj_set_pos(label_top, 80, 150);

    //游戏结束
    if (game_over == 1)
    {
        for(int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                lv_obj_del(btn[i][j]);
            }
        }
    }
}

